/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package titanisu.basicMathModule1.DecimalMultAndDivby10mult;

import com.sun.script.javascript.RhinoScriptEngine;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptException;

/**
 *
 * @author Balaji
 */
public class QuestionGeneratorAddSub {

    ArrayList quest = new ArrayList();
    boolean intQ = false;

    private void setQ(ArrayList arlst) {
        this.quest = arlst;
    }

    public ArrayList getQuest() {
        return quest;
    }

    public void setQuest(ArrayList quest) {
        this.quest = quest;
    }

    /*
     * Generates a single question in below format is forms and array with question and nominator and denominator
     * The final the arraylist is put in variable quest and this methog returns a string putting forward only question
     * quest.get(0) question string
     * quest.get(1) answer nominator
     * quest.get(0) answer denominator
     * 
     * @param: void
     * @returns : String having question
     */
    public String getQuestions() {
        try {
            int x = new Random().nextInt(9) + 1;
            int y = (int) Math.pow(10, new Random().nextInt(7));

            String op = new titanisu.templates.helperClasses.VariablesHelper().getRandomOperator("X/");

            String xString = "" + x + "." + (new Random().nextInt(99998) + 1), yString = "" + y;
            if (intQ) {
                if (Math.random() > .5) {
                    xString = "(-" + x + "." + String.valueOf(new Random().nextInt(98) + 1) + ")";
                }

                if (Math.random() > .5) {
                    yString = "(-" + y + ")";
                }
            }


            if (Math.random() > .5) {
                quest.add(0, xString + " " + op + " " + yString);
            } else {
                if (yString.length() > 2 && op.indexOf("/") != -1) {
                    quest.add(0, (int) Math.pow(10, new Random().nextInt(2)) + "" + " " + op + " " + xString);
                } else {
                    quest.add(0, yString + " " + op + " " + xString);
                }
            }

            com.sun.script.javascript.RhinoScriptEngine se = new RhinoScriptEngine();
            String exp = (String) quest.get(0);
            String ev = se.eval(exp.replaceAll("X", "*") + ";").toString();

            if (ev.endsWith(".0")) {
                ev = ev.substring(0, ev.length() - 2);
            }

            int decLoc = ev.indexOf(".");
            if (decLoc != -1) {
                if (ev.substring(decLoc).length() > 4) {
                    ev = ev.substring(0, decLoc + 3);
                }
            }
            quest.add(1, ev);
            setQ(quest);
            //        System.out.println(" quest set in questionGenerator is " + quest);

        } catch (ScriptException ex) {
            Logger.getLogger(QuestionGeneratorAddSub.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(" the quest is " + quest);
        return (String) quest.get(0);
    }

    /**
     * Gives the answers
     *
     * @return
     */
    public String getAnswer() {
        String answer = "";
        String den = (String) quest.get(1);
        return answer;
    }

    void setIntegers(boolean b) {
        intQ = b;
    }
}
